package com.kuaimacode.kframework.agent.services.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.agent.vo.request.OrderReq;
import com.kuaimacode.kframework.mybatis.mapper.user.OrderMapper;
import com.kuaimacode.kframework.mybatis.models.user.Order;
import com.kuaimacode.kframework.mybatis.models.user.OrderNear;
import com.kuaimacode.kframework.mybatis.models.user.OrderSimple;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * @author 中元
 * @date 2019/03/18
 */
@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> {

    	/**
    	 *  分页查找所有的数据
    	 * @param pageNum 当前页码，从1开始
    	 * @param pageSize  一页显示多少行
    	 * @return
    	 */
    	public PageInfo<Order> findAll(int pageNum, int pageSize) {
    		PageHelper.startPage(pageNum, pageSize);
    		List<Order> datas = baseMapper.selectList(null);
    		return new PageInfo<Order>(datas);
    	}

	/**
	 *  分页查找所有的数据(按条件)
	 * @param pageNum 当前页码，从1开始
	 * @param pageSize  一页显示多少行
	 * @return
	 */
	public PageInfo<Order> findAll(int pageNum, int pageSize, OrderReq order) {
		PageHelper.startPage(pageNum, pageSize);
		QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
		Optional.ofNullable(order.getAgentId()).filter(str->!"".equals(str)).ifPresent(str->queryWrapper.eq("agent_id",str));
		Optional.ofNullable(order.getOrderNo()).filter(str->!"".equals(str)).ifPresent(str->queryWrapper.eq("order_no",str));
		Optional.ofNullable(order.getType()).ifPresent(str->queryWrapper.eq("type",str));
		Optional.ofNullable(order.getChildType()).ifPresent(str->queryWrapper.eq("child_type",str));
		Optional.ofNullable(order.getStatus()).ifPresent(str->queryWrapper.eq("status",str));
		Optional.ofNullable(order.getPayType()).ifPresent(str->queryWrapper.eq("pay_type",str));
		Optional.ofNullable(order.getEndDate()).ifPresent(str->queryWrapper.le("create_at",str));
		Optional.ofNullable(order.getStartDate()).ifPresent(str->queryWrapper.ge("create_at",str));
        Optional.ofNullable(order.getMasterId()).ifPresent(str->queryWrapper.ge("master_id",str));
        queryWrapper.orderByDesc("id");//降序
		List<Order> datas = baseMapper.selectList(queryWrapper);
		return new PageInfo<Order>(datas);
	}

    /**
     * 今日订单数
     * @return
     */
    public int todayOrderCount() {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime beginTime = now.withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endTime = now.withHour(23).withMinute(59).withSecond(59);

        wrapper.between("create_at", beginTime, endTime);
        wrapper.eq("agent_id", SessionUtil.getAgentId());
        return baseMapper.selectCount(wrapper);
    }

    /**
     * 今日订单金额
     * @return
     */
    public long todayOrderAmount() {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime beginTime = now.withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endTime = now.withHour(23).withMinute(59).withSecond(59);

        wrapper.between("create_at", beginTime, endTime);
        wrapper.eq("agent_id", SessionUtil.getAgentId());
        List<Order> orders = baseMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(orders)) {
            return 0;
        }
        BigDecimal amount = new BigDecimal(0);
        for (Order order : orders) {
            amount = amount.add(order.getAmount());
        }
        return amount.longValue();
    }

    public List<OrderNear>  getOrderByStatusList(Long userId, List<Integer> statusList){
        return baseMapper.findOrderByStatusList(statusList,userId);
    }

    /**
     * 获取最新的订单
     * @return
     */
    public List<Order> top10Order() {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_at");
        wrapper.eq("agent_id", SessionUtil.getAgentId());
        PageHelper.startPage(1, 10);
        return baseMapper.selectList(wrapper);
    }


    /**
     *  分页查找所有的数据
     * @param pageNum 当前页码，从1开始
     * @param pageSize  一页显示多少行
     * @param type
     * @return
     */
    public PageInfo<OrderSimple> queryAllOrders(Long userId, Integer status, Integer type, int pageNum, int pageSize, String agentId) {
        PageHelper.startPage(pageNum, pageSize,true,false,null);
        List<OrderSimple> datas = baseMapper.findAllOrders(status, userId, type, agentId);
        return new PageInfo<OrderSimple>(datas);
    }

    /**
     *  查找所有的数据
     * @param type
     * @return
     */
    public List<OrderSimple> queryAllOrdersNoPage(Long userId, Integer status, Integer type, String agentId) {
        List<OrderSimple> datas = baseMapper.findAllOrders(status, userId, type, agentId);
        return datas;
    }


    /**
     * 获取所有师傅的已接订单
     * @return
     */
    /*public List<Order> findMasterOrders(Long masterId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("master_id", masterId);
        Integer[] status = {3,4,5};//服务中、取货中、送货中
        wrapper.in("status", status);
        return baseMapper.selectList(wrapper);
    }*/
    public List<OrderSimple> findMasterOrders(Long masterId) {
        List<OrderSimple> datas = baseMapper.findMasterOrders(20, masterId);
        return datas;
    }
}

